//
//  SISSSignatureViewController.m
//  SISSAPP
//
//  Created by 优房网 on 2017/7/12.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSSignatureViewController.h"
#import "SISSDrawSignView.h"
#import "AppDelegate.h"

@interface SISSSignatureViewController ()


/* signTitleLabel */
@property (weak, nonatomic) UILabel *signTitleLabel;
/* signNameLabel */
@property (weak, nonatomic) UILabel *signNameLabel;
@property (weak, nonatomic) SISSDrawSignView *drawView;


@end

@implementation SISSSignatureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"电子签名";
    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithWhite:1 alpha:0];
}

//- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
//    return UIInterfaceOrientationMaskLandscapeLeft;
//}

// 1.视图将要出现的时候
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear: YES];
    AppDelegate * appdelegate =(AppDelegate *) [UIApplication  sharedApplication].delegate;
    appdelegate.Rotate = 1;
    
    // 如果强制横屏,,打开这段代码,,并且将手机设备的竖排方向锁定打开即可
    if ([[UIDevice currentDevice]respondsToSelector:@selector(setOrientation:)]) {
        [self orientationToPortrait:UIInterfaceOrientationLandscapeRight];
    }
}
// 2.视图将要消失的时候
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    AppDelegate * appdelegate = (AppDelegate *)[UIApplication  sharedApplication].delegate;
    appdelegate.Rotate = 0;
    
    if ([[UIDevice currentDevice]respondsToSelector:@selector(setOrientation:)]) {
        [self orientationToPortrait:UIInterfaceOrientationPortrait];
    }
}
- (void)orientationToPortrait:(UIInterfaceOrientation)orientation {
    SEL selector = NSSelectorFromString(@"setOrientation:");
    NSInvocation * invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
    [invocation setSelector:selector];
    [invocation setTarget:[UIDevice currentDevice]];
    int val = orientation;
    [invocation setArgument:&val atIndex:2];
    [invocation invoke];
    
}

#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];
    
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    SISSDrawSignView *drawView = [[SISSDrawSignView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    drawView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:drawView];
    self.drawView = drawView;
    
    UILabel *signTitleLabel = [[UILabel alloc] init];
    signTitleLabel.textColor = MAIN_TEXT_COLOR;
    signTitleLabel.text = [NSString stringWithFormat:@"签署日期：%@",[[NSDate yyr_dateStringWithDelta:0] substringToIndex:10]];
    [self.view addSubview:signTitleLabel];
    self.signTitleLabel = signTitleLabel;
    
    UILabel *signNameLabel = [[UILabel alloc] init];
    signNameLabel.textColor = MAIN_TEXT_COLOR;
    signNameLabel.text = [NSString stringWithFormat:@"签署方：%@",[SISSUserAccount shared].name];
    [self.view addSubview:signNameLabel];
    self.signNameLabel = signNameLabel;
    
    UIButton *clearButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [clearButton setTitle:@"清空" forState:UIControlStateNormal];
    [clearButton setBackgroundColor:COLOR(0x8998A5)];
    [clearButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [clearButton addTarget:self action:@selector(clearButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    clearButton.layer.cornerRadius = 4;
    clearButton.layer.masksToBounds = YES;
    [self.view addSubview:clearButton];
    
    UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [confirmButton setTitle:@"确定" forState:UIControlStateNormal];
    [confirmButton setBackgroundColor:MAIN_COLOR];
    [confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [confirmButton addTarget:self action:@selector(confirmButtonClickAction) forControlEvents:UIControlEventTouchUpInside];
    confirmButton.layer.cornerRadius = 4;
    confirmButton.layer.masksToBounds = YES;
    [self.view addSubview:confirmButton];
    
    WeakSelf
    CGFloat margin = 8.0;
    [signTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0, *)) {
            make.top.equalTo(weakSelf.view.mas_safeAreaLayoutGuideTop).offset(2 * margin);
            make.left.equalTo(weakSelf.view.mas_safeAreaLayoutGuideLeft).offset(2.5 * margin);
        } else {
            make.top.equalTo(weakSelf.mas_topLayoutGuideBottom).offset(2 * margin);
            make.left.equalTo(weakSelf.view).offset(2.5 * margin);
        }
    }];
    [signNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(signTitleLabel);
        make.top.equalTo(signTitleLabel.mas_bottom).offset(margin);
    }];
    
    [self.drawView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view).with.offset(0);
        make.left.equalTo(weakSelf.view);
        make.right.equalTo(weakSelf.view);
        if (@available(iOS 11.0, *)) {
            make.bottom.equalTo(weakSelf.view.mas_safeAreaLayoutGuideBottom);
        } else {
            make.bottom.equalTo(weakSelf.view);
        }
    }];
    
    [clearButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(confirmButton.mas_left).offset(-margin);
        make.size.equalTo(confirmButton);
        if (@available(iOS 11.0, *)) {
            make.bottom.equalTo(weakSelf.view.mas_safeAreaLayoutGuideBottom).offset(-margin);
        } else {
            make.bottom.equalTo(weakSelf.view).offset(-margin);
        }
    }];
    [confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 44));
        if (@available(iOS 11.0, *)) {
            make.right.equalTo(weakSelf.view.mas_safeAreaLayoutGuideRight).offset(-margin);
            make.bottom.equalTo(weakSelf.view.mas_safeAreaLayoutGuideBottom).offset(-margin);
        } else {
            make.right.equalTo(weakSelf.view).offset(-margin);
            make.bottom.equalTo(weakSelf.view).offset(-margin);
        }
    }];
}



#pragma mark - 生成图片
- (void)confirmButtonClickAction {
    
    if (!self.drawView.isDraw) {
        NSLog(@"没有字迹");
        return;
    }
    UIImage *image = [self imageWithUIView:self.drawView size:CGSizeMake(100, 100)];
    self.image = image;
    
    NSString *str = @"本人已明确本合同约定之权利和义务，确定签署。";
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.alignment = NSTextAlignmentLeft;
    
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:str attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:16], NSForegroundColorAttributeName: COLOR(0x334A60), NSParagraphStyleAttributeName: paragraphStyle}];
    
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"签署合同" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    [alertC setValue:attr forKey:@"attributedMessage"];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"取消");
    }];
    
    [alertC addAction:cancelAction];
    
    WeakSelf
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [weakSelf popupConfirmButtonClickAction];
        NSLog(@"确定");
    }];
    [alertC addAction:okAction];
    
    [self presentViewController:alertC animated:YES completion:nil];
    
}


/**
 清除字迹
 */
- (void)clearButtonClickAction {
    
    [self.drawView clear];
}



/**
 根据View生成图片

 @param view view
 @return 生成的图片
 */
- (UIImage*)imageWithUIView:(UIView*)view size:(CGSize)size {
    
//    UIGraphicsBeginImageContext(view.bounds.size);
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0.0);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    [view.layer renderInContext:context];
    
    UIImage* tImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    
    return tImage;
    
}


/**
 确定后弹窗确定按钮点击事件
 */
- (void)popupConfirmButtonClickAction {
    
    
}




@end
